<template>

  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.showClose"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form ref="formBook" :rules="rulesEvent" :model="formEvent" label-width="180px">

      <el-form-item label="事件发生时间" prop="eventTime" >
        <!--这个组件会报 Avoid mutating a prop directly since the,添加 placement="bottom-start" -->
        <el-date-picker
          v-model="formEvent.eventTime"
          type="date"
          placement="bottom-start"
          placeholder="选择发生时间"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="事件内容" prop="eventContent" >
        <el-input v-model.trim="formEvent.eventContent" type="textarea"></el-input>
      </el-form-item>

      <el-form-item label="事件级别">
        <el-select v-model.trim="formEvent.eventLevel" placeholder="请选择事件级别" clearable>
          <el-option label="特别重大事件" :value="10"></el-option>
          <el-option label="重大事件" :value="20"></el-option>
          <el-option label="较大事件" :value="30"></el-option>
          <el-option label="一般事件" :value="40"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="事件发生地点" prop="eventContent" >
        <el-input v-model.trim="formEvent.address"></el-input>
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd" v-if="defaultValue.saveUpdSelect !== 'select'">{{defaultValue.title}}</el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>

</template>

<script>

  import http from '@/utils/http'
  import {clearInvalid, getFullDate} from "../../utils/utils"

  export default {
    name: "EventSaveUpd",
    data(){
      return {
        // 表格的默认值
        tableConfig: {
          // 关闭时销毁 Dialog 中的元素
          destroyOnClose: true,
          // 弹出框是否打开
          show: false,
          // 是否显示关闭按钮
          showClose: true,
          // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
          lockScroll: false,
          appendToBody: true,
        },
        // 默认值，进入页面后的值
        defaultValue: {
          // 弹出框标题
          title: '查看',
          // 保存还是修改or查看,取值有 select | save | upd
          saveUpdSelect: 'select'
        },
        // 提交的数据
        formEvent: {
          eventTime: null,
          eventContent: null,
          eventLevel: null,
          address: null,
        },
        // 校验
        rulesEvent: {
          eventContent: [{ required: true, message: '请输入事件内容', trigger: 'blur' }],
          eventTime: [{ required: true, message: '请输入事件发生时间', trigger: 'blur' }],
        },
      }
    },
    methods: {
      /**
       * 打开弹出框之前
       * @param value 保存还是修改or查看,取值有 select \ save \ upd
       * @param obj 如果是修改和查看，则是详细数据,需要赋值
       */
      openEventPage(value, obj){
        this.defaultValue.saveUpdSelect = value
        this.initEvent()

        if (value === 'save'){
          this.defaultValue.title = '新增事件'
          this.getEventLocale()

        } else if (value === 'upd'){
          this.defaultValue.title = '修改事件'
          this.formEvent = JSON.parse(JSON.stringify(obj))

        } else {

        }

        this.tableConfig.show = true
        this.$store.commit('navigationListChange', ['家庭管理', '事件', '新增', this.defaultValue.title])
      },
      /**
       * 是保存还是修改事件
       */
      toSaveUpd(){
        if (this.defaultValue.saveUpdSelect === 'save'){
          this.savePsEvent()

        } else if (this.defaultValue.saveUpdSelect === 'upd'){
          this.updPsEvent()

        } else {

        }
      },
      /**
       * 保存事件
       */
      savePsEvent(){
        http.post("/event/savePsEvent", this.formEvent).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("保存成功");
          this.$emit('getEventListPage', null)
          // 关闭弹出框
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 修改事件
       */
      updPsEvent(){
        clearInvalid(this.formEvent)
        http.post("/event/updPsEvent", this.formEvent).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("修改成功");
          this.$emit('getEventListPage', null)
          // 关闭弹出框
          this.initEvent()
          this.tableConfig.show = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 获取阅读书籍的地址
       */
      getEventLocale(){
        http.post("/event/getEventLocale", {
        }).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }
          this.formEvent.address = res.data.value

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 重置初始化事件
       */
      initEvent(){
        this.formEvent = {
          eventTime: null,
          eventContent: null,
          eventLevel: null,
          address: null,
        }
      },
    },
  }
</script>

<style scoped>

</style>
